package com.zzzy.doc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zzzy.common.exception.BusinessException;
import com.zzzy.common.util.AssertUtil;
import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.common.enums.DocStatusEnum;
import com.zzzy.doc.dao.entity.DocAttachment;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.dao.mapper.DocAttachmentMapper;
import com.zzzy.doc.vo.document.attachment.DocAttachmentAddReq;
import com.zzzy.doc.vo.document.attachment.DocAttachmentPageReq;
import com.zzzy.doc.vo.pageoffice.OpenOfficeFileRes;
import com.zzzy.framework.common.util.PageOfficeUtil;
import com.zzzy.framework.common.util.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 公文附件
 *
 * @author fc
 * @since 2024-08-21
 */
@Service
public class DocAttachmentService extends ServiceImpl<DocAttachmentMapper, DocAttachment> {

    @Resource
    private DocService docService;

    public PageResult<DocAttachment> getDocAttachmentPage(DocAttachmentPageReq queryReq) {
        docService.validateAuthForQuery(queryReq.getDocId());

        LambdaQueryWrapper<DocAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(DocAttachment::getId, DocAttachment::getName, DocAttachment::getCreateTime);

        wrapper.eq(DocAttachment::getDocId, queryReq.getDocId()).orderByDesc(DocAttachment::getId);
        return baseMapper.selectPage(queryReq, wrapper);
    }

    @Transactional
    public void addDocAttachment(DocAttachmentAddReq addReq) {
        Document document = docService.getDocById(addReq.getDocId());
        AssertUtil.isNotEquals(DocStatusEnum.CANCEL.getValue(), document.getStatus(), "公文状态错误");
        AssertUtil.isEquals(document.getUserId(), SecurityUtils.getLoginUser().getId(), "当前用户无此操作权限");
        byte[] byteFileBlob;
        try {
            byteFileBlob = addReq.getFile().getBytes();
        } catch (IOException e) {
            throw new BusinessException("文件读取错误");
        }

        DocAttachment docAttachment = new DocAttachment();
        docAttachment.setDocId(addReq.getDocId());
        docAttachment.setCreateTime(LocalDateTime.now());
        docAttachment.setName(addReq.getFile().getOriginalFilename());
        docAttachment.setFileBlob(byteFileBlob);
        baseMapper.insert(docAttachment);

    }

    public DocAttachment getSimpleDocAttachmentById(Long id) {
        LambdaQueryWrapper<DocAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(DocAttachment::getId, DocAttachment::getDocId, DocAttachment::getName, DocAttachment::getCreateTime);

        wrapper.eq(DocAttachment::getId, id);
        DocAttachment docAttachment = baseMapper.selectOne(wrapper);
        AssertUtil.nonNull(docAttachment, "附件不存在");
        return docAttachment;
    }

    @Transactional
    public void deleteById(Long id) {
        DocAttachment docAttachment = getSimpleDocAttachmentById(id);
        Document document = docService.getDocById(docAttachment.getDocId());
        AssertUtil.isNotEquals(DocStatusEnum.CANCEL.getValue(), document.getStatus(), "公文状态错误");
        AssertUtil.isEquals(document.getUserId(), SecurityUtils.getLoginUser().getId(), "当前用户无此操作权限");
        baseMapper.deleteById(id);

    }

    public OpenOfficeFileRes getPageOfficeResByDocId(Long id, String openFileUrl) {
        DocAttachment docAttachment = getSimpleDocAttachmentById(id);
        docService.validateAuthForQuery(docAttachment.getDocId());
        String name = docAttachment.getName();
        if (name.endsWith(FileUtil.DOC) || name.endsWith(FileUtil.DOCX)){
            String wordHtml = PageOfficeUtil.getWordHtml(openFileUrl, name, OpenModeType.docReadOnly);
            return OpenOfficeFileRes.builder().html(wordHtml).build();
        } else if (name.endsWith(FileUtil.XLS) || name.endsWith(FileUtil.XLSX)){
            String excelHtml = PageOfficeUtil.getExcelHtml(openFileUrl, name, OpenModeType.xlsReadOnly);
            return OpenOfficeFileRes.builder().html(excelHtml).build();
        } else {
            String pdfHtml = PageOfficeUtil.getPdfHtml(openFileUrl, name);
            return OpenOfficeFileRes.builder().html(pdfHtml).pdf(true).build();
        }
    }

    public DocAttachment getDocAttachmentById(Long id) {
        DocAttachment docAttachment = baseMapper.selectById(id);
        AssertUtil.nonNull(docAttachment, "附件不存在");
        docService.validateAuthForQuery(docAttachment.getDocId());
        return docAttachment;
    }
}
